package cn.goktech.sports.modules.importDataone.controller;

import cn.goktech.sports.common.annotation.RestAnon;
import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.MD5Utils;
import cn.goktech.sports.modules.importDataone.entity.*;
import cn.goktech.sports.modules.importDataone.service.UserService;
import cn.goktech.sports.modules.importDataone.utils.EMSRest;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 〈一句话功能简述〉<br>
 * 〈用户控制器〉
 *
 * @author PanY
 * @create 2019/10/15
 * @since 1.0.0
 */
@RestController
@RequestMapping("rest/user")
public class UserController {
	@Autowired
	UserService userService;

	/**
	 * 功能描述: <br>
	 * 前端登录
	 *
	 * @param name
	 * @param password
	 * @return cn.goktech.sports.common.entity.R
	 * @author PanY
	 * @date 2019/10/16 10:05
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@RestAnon
	@ResponseBody
	@ApiOperation(value = "前端登录")
	public R login(@RequestParam("name") String name, @RequestParam("password") String password) {
		if (StringUtils.isBlank(name)) {
			return R.error("账号不能为空！");
		}
		if (StringUtils.isBlank(password)) {
			return R.error("密码不能为空！");
		}
		UserEntity userEntity = userService.selectUserByNameAndPwd(name, MD5Utils.encrypt(name, password));
		if (userEntity != null) {
			return R.ok("成功");
		}
		return R.error("账号或密码错误");
	}

	/**
	 * 功能描述: <br>
	 * 计算体积
	 *
	 * @param params
	 * @return cn.goktech.sports.common.entity.R
	 * @author PanY
	 * @date 2019/10/16 10:07
	 */
	@RequestMapping(value = "volume", method = RequestMethod.POST)
	@RestAnon
	public R goodsVolume(@RequestBody Map<String, Object> params) {
		Double aLong = Double.valueOf((String) params.get("long")); //长
		Double wide = Double.valueOf((String) params.get("wide")); //宽
		Double high = Double.valueOf((String) params.get("high")); //高
		//体积计算
		Double l = (aLong * wide * high) / 1000000; //平方米
		BigDecimal b = new BigDecimal(l);
		double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return R.ok(String.valueOf(f1));
	}

	/**
	 * 功能描述: <br>
	 * 原寄地三级联动
	 *
	 * @param
	 * @return cn.goktech.sports.common.entity.R
	 * @author PanY
	 * @date 2019/10/16 11:34
	 */
	@RequestMapping(value = "originalPlace", method = RequestMethod.GET)
	@ResponseBody
	@RestAnon
	@ApiOperation(value = "原寄地三级联动")
	public R originalPlace() {
		List<OriginalPlaceEntity> list = userService.getAreaList();
		Map m = new HashMap();
		m.put("200", list);
		return R.ok(m);
	}

	/**
	 * 功能描述: <br>
	 * 最终查询
	 *
	 * @param params 前端所有数据
	 * @return cn.goktech.sports.common.entity.R
	 * @author PanY
	 * @date 2019/10/22 9:24
	 */
	@RequestMapping(value = "finalQuery", method = RequestMethod.POST)
	@RestAnon
	@ApiOperation("运费计算")
	public R finalQuery(@RequestBody Map<String, Object> params) {
		//判断海空铁陆
		String transport = (String) params.get("transport");
		if (transport.equals("sea")) {//海运
			return seaTransport(params);
		} else if (transport.equals("empty")) {//空运
			return emptyTransport(params);
		} else if (transport.equals("iron")) {//铁运
			return iron_transportation(params);
		} else if (transport.equals("road")) {//陆运
			return landTransport(params);
		} else {
			return R.error("运输方式有误！请选择运输方式");
		}
	}

	@RequestMapping(value = "isProhibited", method = RequestMethod.POST)
	@RestAnon
	@ApiOperation("是否为违禁品")
	public R isProhibited(@RequestBody Map<String, Object> params) {
		//判断海空铁陆
		//仓库Id的为空判定
		Long depotId = 0L;
		if (params.get("depotId") != null) {
			depotId = Long.valueOf((Integer) params.get("depotId"));
		}
		//国家Id的为空判定
		Long countryId = 0L;
		if (params.get("countryId") != null) {
			countryId = Long.valueOf(((Integer) params.get("countryId")));
		}

		if (StringUtils.isBlank((String) params.get("goodsName"))) {
			return R.error("待运输物品不能为空");
		}
		String goodsName = (String) params.get("goodsName");
		return userService.getGoodsNameNum(goodsName, depotId, countryId);
	}

	/**
	 * 铁运
	 */
	public R iron_transportation(Map<String, Object> params) {
		//定义返回的对象
		Map<String, Object> map = new HashMap<>();
		List<EMSRest> list = new ArrayList<>();
		//取出长宽高
		Double aLong = Double.valueOf((String) params.get("long")); //长
		Double wide = Double.valueOf((String) params.get("wide")); //宽
		Double high = Double.valueOf((String) params.get("high")); //高
		String warehouse = (String) params.get("warehouse");//仓库字符串
		//长宽高转换为立方米
		BigDecimal volume = new BigDecimal(Double.toString((aLong * wide * high) / 1000000));
		//查询铁运数据
		List<HeaderRailwayEntity> headerRailway = userService.getHeaderRailway(warehouse);
		if (headerRailway.size() == 0) {
			return R.error("没有查询到通往" + warehouse + "的数据");
		}
		//3.1判断是否满足起运重量
		for (Iterator<HeaderRailwayEntity> it = headerRailway.iterator(); it.hasNext(); ) {
			HeaderRailwayEntity en = it.next();
			//如果重量限制能够转换为数字
			if (NumberUtils.isNumber(en.getVolume_claim())) {
				//如果用户输入的小于起运重量，则移除该产品
				if (volume.compareTo(new BigDecimal(en.getVolume_claim())) == -1) {
					it.remove();
				}
			}
		}
		if (headerRailway.size() == 0) {
			return R.error("没有通往" + warehouse + "的数据");
		}
		for (HeaderRailwayEntity rail : headerRailway) {
			EMSRest ems = new EMSRest();
			ems.setTransport_name(rail.getRailway_name());//运输产品名称
			ems.setFuel_cost(new BigDecimal("0.0"));//燃油费
			ems.setOther_cost(new BigDecimal("0.0"));//其他费用
			ems.setTransport_way("铁运"); //运输方式
			ems.setConsult_aging(rail.getRecent_time());//参考时限
			//查询区间
			List<HeaderRailwaySectionEntity> section = userService.getHeaderRailwaySection(rail.getRailway_id());
			for (HeaderRailwaySectionEntity entity : section) {
				//只是整柜的情况下.判断长、宽、高
				if ((entity.getMin_weight() == null || entity.getMin_weight() == 0) && (entity.getMax_weight() == null || entity.getMax_weight() == 0)
						&& entity.getRailway_bulk_weight() != null) {
					BigDecimal sectionBig = sectionBig(aLong, wide, high, entity);
					if (sectionBig.compareTo(BigDecimal.ZERO) == 0) {
						break;
					} else {
						ems.setConsult_freight(sectionBig);//参考运费
						ems.setTotal_freight(sectionBig);//总运费
						list.add(ems);
					}
					break;
				}
				//判断散柜区间
				if ((entity.getMin_weight() != null || entity.getMin_weight() != 0) && (entity.getMax_weight() != null || entity.getMax_weight() != 0) &&
						(entity.getRailway_bulk_weight() == null || entity.getRailway_bulk_weight() == 0)) {
					//判断铁运区间是否满足
					if (volume.compareTo(new BigDecimal(String.valueOf(entity.getMin_weight()))) == 1
							&& (volume.compareTo(new BigDecimal(String.valueOf(entity.getMax_weight()))) == 0
							|| volume.compareTo(new BigDecimal(String.valueOf(entity.getMax_weight()))) == -1)) {
						//铁运重量进行进位改变
						volume = seaORIronCalculation(volume);
						BigDecimal bigDecimal = volume.multiply(entity.getRailway_price()).setScale(2, BigDecimal.ROUND_HALF_UP);
						ems.setTotal_freight(bigDecimal);   //总运费
						ems.setConsult_freight(bigDecimal); //参考运费
						list.add(ems);
						break;
					}
				}
				//如果有最小的重量，没有最大重量，并且货柜是空
				if ((entity.getMax_weight() == null || entity.getMax_weight() == 0) &&
						(entity.getMin_weight() != null || entity.getMin_weight() != 0) &&
						(entity.getRailway_bulk_weight() == null || entity.getRailway_bulk_weight() == 0)) {
					//判断体积不是整数时进位
					volume = seaORIronCalculation(volume);
					BigDecimal bigDecimal = volume.multiply(entity.getRailway_price()).setScale(2, BigDecimal.ROUND_HALF_UP);
					ems.setTotal_freight(bigDecimal);   //总运费
					ems.setConsult_freight(bigDecimal); //参考运费
					list.add(ems);
					break;
				}

			}
		}
		//进行排序，第一步根据运费排序
		Collections.sort(list);
		List<Object> resList = new ArrayList<>();
		if (list.size() > 0) {
			//存储最后返回给前端的数据
			map.put("ageing", list);
		} else {
			map.put("ageing", resList);
		}
		return R.ok(map);
	}

	/**
	 * 海、铁体积0.5立方进位
	 */
	private BigDecimal seaORIronCalculation(BigDecimal volume) {
		if (new BigDecimal(volume.intValue()).compareTo(volume) == -1) {
			//如果体积不是整数，加上0.5是否大于等于体积舍去小数加上1，如果是则表示该小数位是0.5以上则直接进位为1
			//先把体积的小数位舍去，并加上1
			BigDecimal add = volume.setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("1"));
			BigDecimal add2 = volume.setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("0.5"));
			if (volume.add(new BigDecimal("0.5")).compareTo(add) == 1 ||
					volume.add(new BigDecimal("0.5")).compareTo(add) == 0) {
				//如果原体积加上0.5大于或者等于体积整数加1，则表示需要进位为1；
				volume = add;
			} else if (volume.add(new BigDecimal("0.5")).compareTo(add) == -1 && volume.add(new BigDecimal("0.5")).compareTo(add2) != 0) {
				//反之，则是0.5
				volume = volume.setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("0.5"));
			}
		}
		return volume;
	}

	/**
	 * 海运
	 */
	public R seaTransport(Map<String, Object> params) {
		//定义返回的对象
		Map<String, Object> map = new HashMap<>();
		List<EMSRest> list = new ArrayList<>();
		//取出长宽高
		Double aLong = Double.valueOf((String) params.get("long")); //长
		Double wide = Double.valueOf((String) params.get("wide")); //宽
		Double high = Double.valueOf((String) params.get("high")); //高
		//1、海运只有头程出发，头程只能到尾程的仓库，判断目的地是否选择了仓库
		String warehouse = (String) params.get("warehouse");//仓库字符串
		if (StringUtils.isBlank(warehouse)) {
			return R.error("海运头程只能发往尾程，请选择尾程的仓库");
		}
		//2、查询海运的数据
		List<HeaderSeaEntity> headerSea = userService.getHeaderSea(warehouse);
		if (headerSea.size() == 0) {
			return R.error("没有查询到通往" + warehouse + "的数据");
		}
		//3、长宽高转换为立方米
		BigDecimal volume = new BigDecimal(Double.toString((aLong * wide * high) / 1000000));
		//3.1判断是否满足起运重量
		for (Iterator<HeaderSeaEntity> it = headerSea.iterator(); it.hasNext(); ) {
			HeaderSeaEntity en = it.next();
			//如果重量限制能够转换为数字，则是海运散货
			if (NumberUtils.isNumber(en.getVolume_claim())) {
				//如果用户输入的小于起运重量，则移除该产品
				if (volume.compareTo(new BigDecimal(en.getVolume_claim())) == -1) {
					it.remove();
				}
			}
		}
		if (headerSea.size() == 0) {
			return R.error("没有通往" + warehouse + "的数据");
		}
		//4、第一层循环 通往该仓库的产品
		for (HeaderSeaEntity sea : headerSea) {
			//5、根据海运查询对应的价格区间
			List<ShippingCharging> headerSeaCharging = userService.getHeaderSeaCharging(sea.getSea_id());
			//6、第二循环，该仓库的价格
			for (ShippingCharging shipp : headerSeaCharging) {
				//声明返回实体对象
				EMSRest ems = new EMSRest();
				ems.setTransport_name(sea.getShipping_name());//运输产品名称
				ems.setFuel_cost(new BigDecimal("0.0"));//燃油费
				ems.setOther_cost(new BigDecimal("0.0"));//其他费用
				ems.setTransport_way("海运"); //运输方式
				ems.setConsult_aging(sea.getRecent_time());//参考时限
				//只是整柜的情况下
				if (shipp.getMin_weight() == null && shipp.getMax_weight() == null
						&& shipp.getShipping_bulk_weight() != null) {
					BigDecimal sectionBig = sectionBigHai(aLong, wide, high, shipp);
					if (sectionBig.compareTo(BigDecimal.ZERO) == 0) {
						break;
					} else {
						ems.setConsult_freight(sectionBig);//参考运费
						ems.setTotal_freight(sectionBig);//总运费
						list.add(ems);
					}
					break;
				}
				//散货情况-散货是1立方起运，0.5立方进位
				if (shipp.getMax_weight() == null && shipp.getMin_weight() != null &&
						shipp.getShipping_bulk_weight() == null) {
					//判断用户输入的体积是否大于最小体积，是则体积乘以价格
					if (volume.compareTo(new BigDecimal(Double.toString(shipp.getMin_weight()))) == 1) {
						//判断体积不是整数时
						volume = seaORIronCalculation(volume);
						//先体积根据规则转换，在乘以价格
						BigDecimal bigDecimal = volume.multiply(shipp.getShipping_price()).setScale(2, BigDecimal.ROUND_HALF_UP);
						ems.setConsult_freight(bigDecimal);//参考运费
						ems.setTotal_freight(bigDecimal);//总运费
						list.add(ems);
						break;
					}
				}
				if (shipp.getMax_weight() != null && shipp.getMin_weight() != null && shipp.getShipping_bulk_weight() == null) {
					if (volume.compareTo(new BigDecimal(Double.toString(shipp.getMin_weight()))) == 1
							&& (volume.compareTo(new BigDecimal(Double.toString(shipp.getMax_weight()))) == 0
							|| volume.compareTo(new BigDecimal(Double.toString(shipp.getMax_weight()))) == -1)) {
						//判断体积不是整数时
						volume = seaORIronCalculation(volume);
						BigDecimal bigDecimal = volume.multiply(shipp.getShipping_price()).setScale(2, BigDecimal.ROUND_HALF_UP);
						ems.setConsult_freight(bigDecimal);//参考运费
						ems.setTotal_freight(bigDecimal);//总运费
						list.add(ems);
						break;
					}
				}
			}
		}
		//进行排序，第一步根据运费排序
		Collections.sort(list);
		List<Object> resList = new ArrayList<>();
		if (list.size() > 0) {
			//存储最后返回给前端的数据
			map.put("ageing", list);
		} else {
			map.put("ageing", resList);
		}
		return R.ok(map);
	}

	/**
	 * list集合排序
	 */
	private void ListSort(List list) {
		Collections.sort(list, new Comparator<EMSRest>() {
			@Override
			public int compare(EMSRest o1, EMSRest o2) {
				//判断时效是否为空，如果为空则是0
				String s1 = StringUtils.isBlank(o1.getConsult_aging()) ? "0" : o1.getConsult_aging();
				String s2 = StringUtils.isBlank(o2.getConsult_aging()) ? "0" : o2.getConsult_aging();
				Integer one = 0;
				Integer two = 0;
				if (!"0".equals(s1)) {//不是0则表示有时效
					one = Integer.parseInt(s1.split("-")[0]);
				} else {
					one = Integer.parseInt(s1);
				}
				if (!"0".equals(s2)) {
					two = Integer.parseInt(s2.split("-")[0]);
				} else {
					two = Integer.parseInt(s2);
				}
				if (one - two > 0) return 1;
				if (one - two < 0) return -1;
				return 0;
			}
		});
	}

	/**
	 * 铁运计算价格
	 */
	private BigDecimal sectionBig(Double aLong, Double wide, Double high, HeaderRailwaySectionEntity entity) {
		//厘米转米
		BigDecimal chang = new BigDecimal(Double.toString(aLong / 100)).setScale(3, BigDecimal.ROUND_HALF_UP);
		BigDecimal kuan = new BigDecimal(Double.toString(wide / 100)).setScale(3, BigDecimal.ROUND_HALF_UP);
		BigDecimal gao = new BigDecimal(Double.toString(high / 100)).setScale(3, BigDecimal.ROUND_HALF_UP);

		BigDecimal bigDecimal = new BigDecimal("0");
		//先判断规格
		if (entity.getRailway_bulk_weight() == 20
				&& "GP".equals(entity.getUnit())) {
			//在判断是否可以直接装下
			if ((chang.compareTo(new BigDecimal("5.898")) == 0 || chang.compareTo(new BigDecimal("5.898")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.385")) == 0 || gao.compareTo(new BigDecimal("2.385")) == -1)) {
				bigDecimal = entity.getRailway_price();
				return bigDecimal;
			}
		}
		if (entity.getRailway_bulk_weight() == 40
				&& "GP".equals(entity.getUnit())) {
			if ((chang.compareTo(new BigDecimal("12.032")) == 0 || chang.compareTo(new BigDecimal("12.032")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.385")) == 0 || gao.compareTo(new BigDecimal("2.385")) == -1)) {
				bigDecimal = entity.getRailway_price();
				return bigDecimal;
			}
		}
		if (entity.getRailway_bulk_weight() == 40
				&& "HQ".equals(entity.getUnit())) {
			if ((chang.compareTo(new BigDecimal("12.032")) == 0 || chang.compareTo(new BigDecimal("12.032")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.69")) == 0 || gao.compareTo(new BigDecimal("2.69")) == -1)) {
				bigDecimal = entity.getRailway_price();
				return bigDecimal;
			}
		}
		if (entity.getRailway_bulk_weight() == 45
				&& "HQ".equals(entity.getUnit())) {
			if ((chang.compareTo(new BigDecimal("13.556")) == 0 || chang.compareTo(new BigDecimal("13.556")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.698")) == 0 || gao.compareTo(new BigDecimal("2.698")) == -1)) {
				bigDecimal = entity.getRailway_price();
				return bigDecimal;
			}
		}
		return bigDecimal;
	}

	/**
	 * 海运价格计算
	 */
	private BigDecimal sectionBigHai(Double aLong, Double wide, Double high, ShippingCharging shipp) {
		//厘米转米
		BigDecimal chang = new BigDecimal(Double.toString(aLong / 100)).setScale(3, BigDecimal.ROUND_HALF_UP);
		BigDecimal kuan = new BigDecimal(Double.toString(wide / 100)).setScale(3, BigDecimal.ROUND_HALF_UP);
		BigDecimal gao = new BigDecimal(Double.toString(high / 100)).setScale(3, BigDecimal.ROUND_HALF_UP);
		BigDecimal bigDecimal = new BigDecimal("0");
		//先判断规格
		if (shipp.getShipping_bulk_weight() == 20
				&& "GP".equals(shipp.getUnit())) {
			//在判断是否可以直接装下
			if ((chang.compareTo(new BigDecimal("5.898")) == 0 || chang.compareTo(new BigDecimal("5.898")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.385")) == 0 || gao.compareTo(new BigDecimal("2.385")) == -1)) {
				bigDecimal = shipp.getShipping_price();
				return bigDecimal;
			}
		}
		if (shipp.getShipping_bulk_weight() == 40
				&& "GP".equals(shipp.getUnit())) {
			if ((chang.compareTo(new BigDecimal("12.032")) == 0 || chang.compareTo(new BigDecimal("12.032")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.385")) == 0 || gao.compareTo(new BigDecimal("2.385")) == -1)) {
				bigDecimal = shipp.getShipping_price();
				return bigDecimal;
			}
		}
		if (shipp.getShipping_bulk_weight() == 40
				&& "HQ".equals(shipp.getUnit())) {
			if ((chang.compareTo(new BigDecimal("12.032")) == 0 || chang.compareTo(new BigDecimal("12.032")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.69")) == 0 || gao.compareTo(new BigDecimal("2.69")) == -1)) {
				bigDecimal = shipp.getShipping_price();
				return bigDecimal;
			}
		}
		if (shipp.getShipping_bulk_weight() == 45
				&& "HQ".equals(shipp.getUnit())) {
			if ((chang.compareTo(new BigDecimal("13.556")) == 0 || chang.compareTo(new BigDecimal("13.556")) == -1)
					&& (kuan.compareTo(new BigDecimal("2.352")) == 0 || kuan.compareTo(new BigDecimal("2.352")) == -1) &&
					(gao.compareTo(new BigDecimal("2.698")) == 0 || gao.compareTo(new BigDecimal("2.698")) == -1)) {
				bigDecimal = shipp.getShipping_price();
				return bigDecimal;
			}
		}
		return bigDecimal;
	}

	/**
	 * 空运
	 */
	public R emptyTransport(Map<String, Object> params) {
		//1、空运有头程出发和国际直达，判断目的地是否选择了仓库
		String warehouse = (String) params.get("warehouse");//仓库字符串
		String goodsName = (String) params.get("goodsName");//物品名称
		String diqu = (String) params.get("oldAddress");    //原寄地
		String country = (String) params.get("country");//国家
		Double weight = Double.valueOf((String) params.get("weight"));//重量
		Double aLong = Double.valueOf((String) params.get("long")); //长
		Double wide = Double.valueOf((String) params.get("wide")); //宽
		Double high = Double.valueOf((String) params.get("high")); //高
		//2、没有仓库，为空则是直达
		if (StringUtils.isBlank(warehouse)) {
			//中速DHL单算。中速FedEx单算
			//定义返回的对象
			Map<String, Object> map = new HashMap<>();
			List<EMSRest> list = new ArrayList<>();

			//3、根据国家查询直达的数据(国际EMS，E邮宝，E特快，中速-TNT等都可能会到用户输入的地方)
			List<NationalWeightEntity> nationAreaList = userService.getNationAreaList(country);
			if (nationAreaList.size() == 0) {
				return R.error("没有找到通往：" + country + " 的产品");
			}
			//4、根据规则验证，并移除某些数据
			nationAreaList = verifyList(nationAreaList, weight, aLong, high, wide, diqu);
			if (nationAreaList.size() == 0) {
				return R.error("没有符合重量或者尺寸的数据！");
			}

			//6、判断运输的东西是文件还是物品
			if (goodsName.contains("发票") || goodsName.contains("单据")
					|| goodsName.contains("票") || goodsName.contains("书") || goodsName.contains("纸") || goodsName.contains("文件")) {
				//7、运输的是文件--先根据规则确定重量应该取体积还是用户输入的
				for (NationalWeightEntity en : nationAreaList) {
					//E邮宝，不建议走文件
					if (en.getArticle_type_id() == 2) {
						continue;
					}
					/******判断重量-定义最终的重量*******/
					BigDecimal totl_weight;
					//1、国际EMS。2、E邮宝。3、E特快。4、航空平常小包。5、中速-DHL。6、中速-TNT。7、航空挂号/跟踪小包。8、FedEx
					//确定重量(国际EMS单边超过60计算体积重量，判断体积重量和实体重量那个大用谁)
					totl_weight = Final_weight(en, aLong, wide, high, weight);
					if (totl_weight.compareTo(BigDecimal.ZERO) == 0) {
						//如果最终的重量为0，体积重量不大于实际重量或者没有，采用用户输入的重量
						totl_weight = new BigDecimal(Double.toString(weight));
					}
					//如果为文件并且重量没有达到DHL的规则按物品进行计算，如要对重量进行进位
					if (en.getArticle_type_id() == 5 && totl_weight.compareTo(new BigDecimal(String.valueOf(en.getFile_weight()))) != 1) {
						totl_weight = DHLOrFedExcarry(totl_weight);
					}
					//9、总重量减去首重重量就是续重重量
					BigDecimal subtract = totl_weight.subtract(new BigDecimal(Double.toString(en.getFirst_weight()))).setScale(2, BigDecimal.ROUND_HALF_UP);

					//10、文件的首重价格
					BigDecimal file_price;
					file_price = getFirst_price(en, totl_weight);

					/******* 续重了多少 ******/
					BigDecimal bigDecimal1 = new BigDecimal("0");
					//特殊情况下，e邮宝，没有续重
					if (en.getContinuous_weight_num() != 0) {
						//重量除以续重的重量
						bigDecimal1 = subtract.divide(new BigDecimal(Double.toString(en.getContinuous_weight_num())))
								.setScale(2, BigDecimal.ROUND_HALF_UP);
					}

					/********** 11、文件续重的价格 = 续重的重量 * 续重价格 ***********/
					BigDecimal multiply = new BigDecimal("0");
					//当续重不为空时，计算续重的价格，遇到不区分文件物品的产品时，使用物品续重价格
					if (bigDecimal1.compareTo(BigDecimal.ZERO) != 0) {
						if (en.getFile_heavy_price() == null) {
							multiply = bigDecimal1.multiply(en.getGoods_heavy_price()).setScale(2, BigDecimal.ROUND_HALF_UP);
						} else {
							multiply = bigDecimal1.multiply(en.getFile_heavy_price()).setScale(2, BigDecimal.ROUND_HALF_UP);
						}
					}


					BigDecimal bigDecimal;
					//12、文件总价格为  首重价格+续重价格
					if (multiply.compareTo(BigDecimal.ZERO) == 0) {
						if (subtract.compareTo(BigDecimal.ZERO) == 0) {
							bigDecimal = file_price;
						} else {
							//E邮宝是直接价格乘以重量
							bigDecimal = file_price.multiply(totl_weight).setScale(2, BigDecimal.ROUND_HALF_UP);
						}
					} else {
						bigDecimal = file_price.add(multiply).setScale(2, BigDecimal.ROUND_HALF_UP);//保留两位少数，四舍五入
					}
					//如果为中速DHL或者FedEx，有重量达到多少采用计费区间计费
					if (en.getArticle_type_id() == 5) {
						if (totl_weight.compareTo(new BigDecimal(String.valueOf(en.getFile_weight()))) == 1) {
							bigDecimal = getDhlPrice(en, totl_weight);
						}
					}
					if (en.getArticle_type_id() == 8 || en.getArticle_type_id() == 9) {
						bigDecimal = getFedExPrice(en, totl_weight);
					}
					//实例化输出类
					EMSRest ems = new EMSRest();
					ems.setType(en.getType());
					ems.setTransport_name(en.getTransport_name());
					ems.setConsult_freight(bigDecimal);//参考运费
					if (en.getSurcharge() != null) {
						ems.setOther_cost(en.getSurcharge());//其他费用
					} else {
						ems.setOther_cost(new BigDecimal("0.0"));//其他费用
					}
					if (en.getFuel_cost() != null) {
						//价格乘以百分比减去原价格为燃油费
						BigDecimal scale = bigDecimal.multiply(en.getFuel_cost()).subtract(bigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
						ems.setFuel_cost(scale);//燃油费
					} else {
						ems.setFuel_cost(new BigDecimal("0.0"));//燃油费
					}
					ems.setTotal_freight(bigDecimal.add(ems.getOther_cost()).add(ems.getFuel_cost()).setScale(2, BigDecimal.ROUND_HALF_UP));//总运费
					ems.setTransport_way("空运"); //运输方式
					ems.setConsult_aging(en.getConsult_extension());//参考时限
					list.add(ems);
				}
				//13、进行排序，第一步根据运费排序
				Collections.sort(list);
				//存储最后返回给前端的数据
				map.put("ageing", list);
				return R.ok(map);
			} else {
				//7、物品
				//查询出来是多条信息
				for (NationalWeightEntity en : nationAreaList) {
					BigDecimal totl_weight;//最终的重量
					//8、根据规则计算重量。国际EMS体积重量（长*宽*高/6000）
					totl_weight = Final_weight(en, aLong, wide, high, weight);
					if (totl_weight.compareTo(BigDecimal.ZERO) == 0) {
						totl_weight = new BigDecimal(String.valueOf(weight));
					}
					//9、总重量减去首重重量就是续重重量
					BigDecimal subtract = totl_weight.subtract(new BigDecimal(Double.toString(en.getFirst_weight())));

					//10、物品的首重价格
					BigDecimal goods_price = en.getGoods_price();
					//续重了多少
					BigDecimal bigDecimal1 = new BigDecimal("0");
					//特殊情况下，e邮宝，没有续重
					if (en.getContinuous_weight_num() != 0) {
						//续重除以开始计费的续重，在乘以价格则是续重价格
						bigDecimal1 = subtract.divide(new BigDecimal(String.valueOf(en.getContinuous_weight_num()))).setScale(2, BigDecimal.ROUND_HALF_UP);
					}
					//11、物品续重的价格 续重重量*续重价格
					BigDecimal multiply = new BigDecimal("0");
					if (bigDecimal1.compareTo(BigDecimal.ZERO) != 0) {
						if (en.getGoods_heavy_price() != null) {
							multiply = bigDecimal1.multiply(en.getGoods_heavy_price());
						}
					}
					//12、物品总价格为  首重价格+续重价格(直达么有附加费，燃油费)
					BigDecimal bigDecimal;
					if (multiply.compareTo(BigDecimal.ZERO) == 0) {
						if (subtract.compareTo(BigDecimal.ZERO) == 0) {
							bigDecimal = goods_price;
						} else {
							bigDecimal = goods_price.multiply(totl_weight).setScale(2, BigDecimal.ROUND_HALF_UP);
						}
					} else {
						bigDecimal = multiply.add(goods_price).setScale(2, BigDecimal.ROUND_HALF_UP);
					}
					//如果为中速DHL或者FedEx，有重量达到多少采用计费区间计费
					if (en.getArticle_type_id() == 5) {
						bigDecimal = getDhlPrice(en, totl_weight);
					}
					if (en.getArticle_type_id() == 8 || en.getArticle_type_id() == 9) {
						bigDecimal = getFedExPrice(en, totl_weight);
					}
					//实例化输出类
					EMSRest ems = new EMSRest();
                    ems.setType(en.getType());
					ems.setTransport_name(en.getTransport_name());
					ems.setConsult_freight(bigDecimal);//参考运费
					if (en.getSurcharge() != null) {
						ems.setOther_cost(en.getSurcharge());//其他费用
					} else {
						ems.setOther_cost(new BigDecimal("0.0"));//其他费用
					}
					if (en.getFuel_cost() != null) {
						//价格乘以百分比减去原价格为燃油费
						BigDecimal scale = bigDecimal.multiply(en.getFuel_cost()).subtract(bigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
						ems.setFuel_cost(scale);//燃油费
					} else {
						ems.setFuel_cost(new BigDecimal("0.0"));//燃油费
					}
					ems.setTotal_freight(bigDecimal.add(ems.getOther_cost()).add(ems.getFuel_cost()).setScale(2, BigDecimal.ROUND_HALF_UP));//总运费
					ems.setTransport_way("空运"); //运输方式
					ems.setConsult_aging(en.getConsult_extension());//参考时限
					list.add(ems);
				}
				//13、进行数据筛选 -运费最少
				Collections.sort(list);
				map.put("ageing", list);
				return R.ok(map);
			}
		} else {
			//定义返回的对象
			Map<String, Object> map = new HashMap<>();
			List<EMSRest> EMSlist = new ArrayList<>();
			//2、仓库不为空，走头程出发，根据仓库名，查询通往仓库的头程数据
			List<HeadProcessEntity> list = userService.getHeadProcess(warehouse);
			if (list.size() == 0) {
				return R.error("没有查询到通往该仓库的数据");
			}
			//3、判断尺寸
			for (Iterator<HeadProcessEntity> iterator = list.iterator(); iterator.hasNext(); ) {
				HeadProcessEntity next = iterator.next();
				//判断起运重量 实际重量小于起运重量则该产品不会运输
				if (weight < Double.valueOf(next.getBilling_weight())) {
					iterator.remove();
					continue;
				}
				BigDecimal zong;
				//重量按1进位
				if (next.getService_type().contains("UPS") || next.getService_type().contains("DHL")
						|| next.getService_type().contains("UPS") || next.getService_type().contains("FEDEX")) {
					//0.5进位
					zong = seaORIronCalculation(new BigDecimal(Double.toString(weight)));
				} else {
					zong = new BigDecimal(Double.toString(weight)).setScale(0, BigDecimal.ROUND_UP);
				}
				BigDecimal priceBig = new BigDecimal("0");
				//根据头程查询计费区间
				List<ChargingSectionEntity> sectionList = userService.getChargingSectionList(next.getHeader_id());
				if (sectionList.size() == 0) {
					//如果某个产品没有费用区间信息，则跳过该路线
					iterator.remove();//移除该项
					continue;
				}
				for (ChargingSectionEntity en : sectionList) {
					//快递的计费区间和其他的不同
					if (next.getService_type().contains("快递")) {
						if (en.getMax_weight() == null) {
							if (zong.compareTo(en.getMin_weight()) == 1 || zong.compareTo(en.getMin_weight()) == 0) {
								priceBig = en.getPrice();
								break;
							}
						} else {
							if (zong.compareTo(en.getMin_weight()) == 1 || zong.compareTo(en.getMin_weight()) == 0
									&& (zong.compareTo(en.getMax_weight()) == -1)) {
								priceBig = en.getPrice();
								break;
							}
						}
					} else {//这是空+派等
						//重量是否大于等于最小重量，并且小于等于最大重量，如果是则取出该区间的价格
						if (en.getMax_weight() == null) {
							if (zong.compareTo(en.getMin_weight()) == 1 || zong.compareTo(en.getMin_weight()) == 0) {
								priceBig = en.getPrice();
								break;
							}
						} else {
							if (zong.compareTo(en.getMin_weight()) == 1 || zong.compareTo(en.getMin_weight()) == 0
									&& (zong.compareTo(en.getMax_weight()) == -1 || zong.compareTo(en.getMax_weight()) == 0)) {
								priceBig = en.getPrice();
								break;
							}
						}
					}
				}
				if (priceBig.compareTo(BigDecimal.ZERO) == 0) {
					//如果价格为0则表示有问题，要么是起运重量小于了计费区间的最低重量
					//跳出本次循环
					iterator.remove();
					continue;
				}
				//总价格
				BigDecimal multiply = zong.multiply(priceBig).setScale(2, BigDecimal.ROUND_HALF_UP);

				EMSRest ems = new EMSRest();

				ems.setTransport_name(next.getService_type());  //名称
				ems.setConsult_freight(multiply); //参考运费
				ems.setTransport_way("空运");
				ems.setConsult_aging(next.getReference_aging());    //时效
                /*if (next.getIs_fuel_cost() == 0) {//燃油费 为0 包含燃油费
                    ems.setFuel_cost(new BigDecimal("0.0"));
                } else {
                    //根据燃油费ID查询燃油费率或者具体的燃油费
                    Map<String, Object> fuel = userService.getFuel(next.getFuel_id());
                    //费率
                    BigDecimal rate = new BigDecimal(String.valueOf(fuel.get("rate")));
                    //价格
                    BigDecimal price = new BigDecimal(String.valueOf(fuel.get("price")));
                    //如果费率为0直接取价格
                    if (rate.compareTo(BigDecimal.ZERO) == 0) {
                        ems.setFuel_cost(price.setScale(2, BigDecimal.ROUND_HALF_UP));
                    }else{
                        ems.setFuel_cost(multiply.multiply(rate).subtract(multiply).setScale(2, BigDecimal.ROUND_HALF_UP));
                    }
                }*/
				ems.setFuel_cost(new BigDecimal("0.0"));
				BigDecimal surcharge = new BigDecimal("0.0");
				ems.setOther_cost(surcharge);//其他费用
				BigDecimal totoal_freight = multiply.add(ems.getFuel_cost()).add(surcharge).setScale(2, BigDecimal.ROUND_HALF_UP);
				ems.setTotal_freight(totoal_freight);
				EMSlist.add(ems);
			}
			//13、进行排序，第一步根据运费排序
			Collections.sort(EMSlist);
			List<Object> resList = new ArrayList<>();
			if (EMSlist.size() > 0) {
				//存储时效的升序
				map.put("ageing", EMSlist);
			} else {
				//存储时效的升序
				map.put("ageing", resList);
			}
			return R.ok(map);
		}
	}

	//直达空运 验证重量尺寸
	private List<NationalWeightEntity> verifyList(List<NationalWeightEntity> nationAreaList, Double weight,
												  Double aLong, Double high, Double wide, String diqu) {
		//4、通过重量限制排除某些产品
		for (Iterator<NationalWeightEntity> it = nationAreaList.iterator(); it.hasNext(); ) {
			NationalWeightEntity en = it.next();
			//4、判断最大重量
			if (en.getWeight_limit_scale() < weight) {
				it.remove();
			}
			//起运重量
			if (weight < en.getFirst_weight()) {
				it.remove();
			}
		}
		//5、判断尺寸，通过尺寸限制，排除某些产品
		for (Iterator<NationalWeightEntity> it = nationAreaList.iterator(); it.hasNext(); ) {
			NationalWeightEntity en = it.next();
			//DHL有不邮寄的地区
			if (en.getArticle_type_id() == 5) {
				String[] strArr = {"广东", "广西", "海南", "江苏", "北京", "天津", "河北", "福建", "浙江", "上海"};
				for (String i : strArr) {
					if (diqu.contains(i)) {
						it.remove();
					}
				}
			}
			//不同产品规则不同1、国际EMS。2、E邮宝。3、E特快。4、航空平常小包。5、中速-DHL。6、中速-TNT。7、航空挂号/跟踪小包
			if (en.getArticle_type_id() == 1 || en.getArticle_type_id() == 3) {
				//任何一边长不超过多少
				if (aLong < en.getMin_long() || aLong > en.getMax_long()
						|| wide < en.getMin_width() || wide > en.getMax_width()
						|| high < en.getMin_high() || high > en.getMax_high()) {
					it.remove();
					continue;
				} else if ((wide + high) * 2 + aLong > en.getStandard_sum_side()) {
					//长度和长度以外的最大横周合计不得超过多少  长+侧面周长
					it.remove();
					continue;
				}
			}
			if (en.getArticle_type_id() == 2) {
				//长不小于14，宽不小于14或者最大超过60厘米，长、宽、高超过90
				if ((aLong < en.getMin_long() || wide < en.getMin_width()) || (aLong > en.getMax_long() || (aLong + wide + high) > en.getTotal_sum())) {
					it.remove();
					continue;
				}
			}
			if (en.getArticle_type_id() == 4 || en.getArticle_type_id() == 7) {
				//长宽高合计不能超过多少
				if ((aLong + wide + high) > en.getTotal_sum()) {
					it.remove();
				}
				//最长一边不能超过多少
				if (aLong > en.getMax_long() || wide > en.getMax_width() || high > en.getMax_high()) {
					it.remove();
				}
				continue;
			}
			if (en.getArticle_type_id() == 6) {
				//长*宽*高 不能超过多少
				if ((aLong * wide * high) > (en.getMax_long() * en.getMax_width() * en.getMax_high())) {
					it.remove();
					continue;
				}
			}
		}
		return nationAreaList;
	}

	//FedEx和DHL的重量进位
	private BigDecimal DHLOrFedExcarry(BigDecimal totl_weight) {
		//1、把原重量小数位直接舍去，并加上1
		BigDecimal add = totl_weight.setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("1"));
		BigDecimal ban = totl_weight.setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("0.5"));
		BigDecimal weight;
		if (totl_weight.add(new BigDecimal("0.5")).compareTo(add) == 1) {
			//如果原体积加上0.5大于了加上1的数据，则表示原数据的小数位大于0.5，计费重量算加一的
			weight = add;
		} else if (totl_weight.add(new BigDecimal("0.5")).compareTo(add) == 0) {
			//如果加上0.5与加一的相等，则表示原数据小数位为0.5直接使用原数据
			weight = totl_weight;
		} else if (totl_weight.add(new BigDecimal("0.5")).compareTo(add) == -1 &&
				totl_weight.add(new BigDecimal("0.5")).compareTo(ban) == 0) {
			//如果加上0.5小于加一，并且与0.5相等，则是一个整数，直接采用整数
			weight = totl_weight;
		} else {
			//反之则小于0.5，则是以0.5进行计算
			weight = totl_weight.setScale(0, BigDecimal.ROUND_DOWN).add(new BigDecimal("0.5"));
		}
		return weight;
	}

	//获得最终重量
	private BigDecimal Final_weight(NationalWeightEntity en, Double aLong, Double wide, Double high, Double weight) {
		//1、国际EMS。2、E邮宝。3、E特快。4、航空平常小包。5、中速-DHL。6、中速-TNT。7、航空挂号/跟踪小包。8、FedEx-经济、9FedEx-优先
		BigDecimal totl_weight = new BigDecimal("0");
		//国际EMS --规则是单边超过60进行记抛
		if (en.getArticle_type_id() == 1 || en.getArticle_type_id() == 3) {
			if (aLong >= en.getMark_throw() || wide >= en.getMark_throw() || high >= en.getMark_throw()) {
				//体积重量
				BigDecimal volume = new BigDecimal(String.valueOf((aLong * wide * high) / 6000));
				//如果体积重量大于用户输入的重量
				if (volume.compareTo(new BigDecimal(String.valueOf(weight))) == 1) totl_weight = volume;
			}
		}
		//中速-DHL、TNT、Fendex 直接判断体积重量和实际重量
		if (en.getArticle_type_id() == 6 || en.getArticle_type_id() == 5 || en.getArticle_type_id() == 8 || en.getArticle_type_id() == 9) {
			//体积重量
			BigDecimal volume = new BigDecimal(String.valueOf((aLong * wide * high) / 5000));
			if (volume.compareTo(BigDecimal.valueOf(weight)) == 1) totl_weight = volume;
		}
		return totl_weight;
	}

	//根据规则得到文件首重价格
	private BigDecimal getFirst_price(NationalWeightEntity en, BigDecimal totl_weight) {
		BigDecimal file_price = new BigDecimal("0");
		//如果遇到文件价格为空的情况下，取物品价格
		if (en.getFile_price() == null) {
			file_price = en.getGoods_price();
		} else {
			file_price = en.getFile_price();
		}
		//如果是中速-TNT判断文件重量
		if (en.getArticle_type_id() == 6) {
			if (totl_weight.compareTo(new BigDecimal(String.valueOf(en.getFile_weight()))) == 1) {
				//文件大于2.5按照物品价格计算
				file_price = en.getGoods_price();
			}
		}
		//如果是中速-DHL判断文件重量
		if (en.getArticle_type_id() == 5) {
			if (totl_weight.compareTo(new BigDecimal(String.valueOf(en.getFile_weight()))) == 1) {
				//文件大于2kg按照物品价格计算
				file_price = en.getGoods_price();
			}
		}
		return file_price;
	}

	//获得DHL的价格
	private BigDecimal getDhlPrice(NationalWeightEntity en, BigDecimal totl_weight) {
		BigDecimal bigDecimal = new BigDecimal("0");
		//判断重量，如果重量大于某个值，采用不同计费区间
		if (totl_weight.compareTo(new BigDecimal(String.valueOf(en.getStart_section()))) == 1) {
			//查询区间-并把总价格赋值
			/***************************************/
			List<Map<String, Object>> throughSection = userService.getThroughSection(en.getCountry_partition_id(), en.getArticle_type_id());
			for (Map<String, Object> ss : throughSection) {
				BigDecimal minWight = new BigDecimal(String.valueOf(ss.get("min_weight")));
				BigDecimal maxWight = new BigDecimal(String.valueOf(ss.get("max_weight")));
				if (totl_weight.compareTo(minWight) == 1 && (totl_weight.compareTo(maxWight) == -1 || totl_weight.compareTo(maxWight) == 0)) {
					BigDecimal price = new BigDecimal(String.valueOf(ss.get("weight_price")));
					//如果在区间内，重量按1进位计算价格
					bigDecimal = totl_weight.setScale(0, BigDecimal.ROUND_UP).multiply(price).setScale(2, BigDecimal.ROUND_HALF_UP);
					break;
				}
			}
		} else {
			//如果没有大于，则采用原始计费区间
			List<Map<String, Object>> dhlCharging = userService.getDhlCharging(en.getCountry_partition_id(), en.getArticle_type_id());
			for (Map<String, Object> dhl : dhlCharging) {
				BigDecimal minWight = new BigDecimal(String.valueOf(dhl.get("min_weight")));    //最小重量
				BigDecimal maxWight = new BigDecimal(String.valueOf(dhl.get("max_weight")));    //最大重量
				boolean b = totl_weight.compareTo(minWight) == 0 || totl_weight.compareTo(minWight) == 1;//大于或等于最小重量
				boolean b1 = totl_weight.compareTo(maxWight) == 0 || totl_weight.compareTo(maxWight) == -1;//小于等于最大重量
				if (b && b1) {//满足条件计算价格
					BigDecimal dhlOrFedExcarry = DHLOrFedExcarry(totl_weight);
					//得到开始价格
					BigDecimal start_price = new BigDecimal(String.valueOf(dhl.get("start_price")));
					//总重量减去开始计费重量-在除以续重重量
					BigDecimal continuous_weight = (dhlOrFedExcarry.subtract(minWight)).divide(new BigDecimal(String.valueOf(dhl.get("continuous_weight"))));
					//乘以续重价格+开始价格=最终价格
					BigDecimal continuous_price = new BigDecimal(String.valueOf(dhl.get("continuous_price")));
					bigDecimal = continuous_weight.multiply(continuous_price).add(start_price).setScale(2, BigDecimal.ROUND_HALF_UP);
				}
			}
		}
		return bigDecimal;
	}

	//获得FedEx的价格
	private BigDecimal getFedExPrice(NationalWeightEntity en, BigDecimal totl_weight) {
		BigDecimal bigDecimal = new BigDecimal("0");
		if (totl_weight.compareTo(new BigDecimal(String.valueOf(en.getStart_section()))) == 1) {
			//查询区间-并把总价格赋值
			/***************************************/
			List<Map<String, Object>> throughSection = userService.getThroughSection(en.getCountry_partition_id(), en.getArticle_type_id());
			for (Map<String, Object> ss : throughSection) {
				BigDecimal price = new BigDecimal(String.valueOf(ss.get("weight_price")));
				if (totl_weight.compareTo(new BigDecimal(String.valueOf(ss.get("min_weight")))) == 1 &&
						(totl_weight.compareTo(new BigDecimal(String.valueOf(ss.get("max_weight")))) == -1 ||
								totl_weight.compareTo(new BigDecimal(String.valueOf(ss.get("max_weight")))) == 0)) {
					//如果在区间内，重量按1进位计算价格
					bigDecimal = totl_weight.setScale(0, BigDecimal.ROUND_UP).multiply(price).setScale(2, BigDecimal.ROUND_HALF_UP);
					break;
				}
			}
		} else {
			BigDecimal dhlOrFedExcarry = DHLOrFedExcarry(totl_weight);
			//查询FedEx价格，根据区id，类型id，重量
			Map<String, Object> fedExCharging = userService.getFedExCharging(en.getCountry_partition_id(), en.getArticle_type_id(), dhlOrFedExcarry);
			bigDecimal = new BigDecimal(String.valueOf(fedExCharging.get("price")));
		}
		return bigDecimal;
	}

	/**
	 * 陆运
	 */
	public R landTransport(Map<String, Object> params) {
		//1、海运只有头程出发，头程只能到尾程的仓库，判断目的地是否选择了仓库
		String warehouse = (String) params.get("warehouse");//仓库字符串

		Double weight = Double.parseDouble(params.get("weight").toString());
		int weights = getCount(weight);
		Map<String, Object> map = new HashMap<>();
		List<EMSRest> EMSlist = new ArrayList<>();
		//根据仓库名，查询通往仓库的头程数据
		List<HeadWayEntity> list = userService.getHeaderLandWay(warehouse, weights);
		if (list.size() == 0) {
			return R.error("没有查询到通往该仓库的数据或重量超标");
		}

		for (int i = 0; i < list.size(); i++) {
			EMSRest emsList = new EMSRest();
			HeadWayEntity headWayEntity = list.get(i);
			emsList.setTransport_name(headWayEntity.getServiceType());
			emsList.setTransport_way("陆运");
			//设置参考时效
			emsList.setConsult_aging(headWayEntity.getReferenceAging());
			//设置燃油费用
			emsList.setFuel_cost(BigDecimal.valueOf(0.0D));
			//设置其它费用
			emsList.setOther_cost(BigDecimal.valueOf(0.0D));
			emsList.setConsult_freight(headWayEntity.getPrice().multiply(BigDecimal.valueOf(weights)));
			//计算总费用。单价x重量.
			emsList.setTotal_freight(headWayEntity.getPrice().multiply(BigDecimal.valueOf(weights)));
			EMSlist.add(emsList);
		}
		//进行排序，根据运费排序
		Collections.sort(EMSlist);
		List<Object> resList = new ArrayList<>();
		if (EMSlist.size() > 0) {
			//存储时效的升序
			map.put("ageing", EMSlist);
		} else {
			//存储时效的升序
			map.put("ageing", resList);
		}
		return R.ok(map);
	}

	/**
	 * 功能描述: <br>
	 * 获取国家列表
	 *
	 * @param
	 * @return cn.goktech.sports.common.entity.R
	 * @author LuoYongJun
	 * @date 2019/10/21 11:34
	 */
	@RequestMapping(value = "baseCountry", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取国家列表")
	@RestAnon
	public R baseCountry() {
		List<BaseCountry> list = userService.getCountryList();
		Map m = new HashMap();
		m.put("200", list);
		return R.ok(m);
	}

	/**
	 * 功能描述: <br>
	 * 获取仓库列表
	 *
	 * @param
	 * @return cn.goktech.sports.common.entity.R
	 * @author LuoYongJun
	 * @date 2019/10/21 11:38
	 */
	@RequestMapping(value = "depotList", method = RequestMethod.GET)
	@ApiOperation(value = "获取仓库列表")
	@ResponseBody
	@RestAnon
	public R depotList(Long countryId) {
		List<Depot> list = userService.getDepotList(countryId);
		Map m = new HashMap();
		m.put("200", list);

		return R.ok(m);
	}


	//Double转int。如果小数部分有数字，值+1.
	public static int getCount(Double val) {
		int i = String.valueOf(val).indexOf(".");
		if (val % Integer.parseInt(String.valueOf(val).substring(0, i)) == 0) {
			return Integer.valueOf(String.valueOf(val).substring(0, i));
		} else {
			return Integer.valueOf(String.valueOf(val + 1).substring(0, i));
		}
	}


}
